package com.tedu.service.impl;

import com.tedu.dao.EmpDao;
import com.tedu.dao.impl.EmpDaoImpl;
import com.tedu.entry.Emp;
import com.tedu.entry.dto.EmpDTO;
import com.tedu.entry.vo.EmpUpdateVO;
import com.tedu.entry.vo.EmpVO;
import com.tedu.service.EmpService;
import com.tedu.util.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Author：毕龙
 * Date: 2023/7/28 10:27
 */
public class EmpServiceImpl implements EmpService {
    //创建dao对象
    private EmpDao empDao = new EmpDaoImpl();

    @Override
    public List<Emp> selectAllEmp() {
        List<Emp> emps = empDao.selectAllEmp();
        return emps;
    }

    @Override
    public int insertEmp(EmpVO empVO) {
        Connection conn = null;

        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            //将empVO的数据疯转到Emp
            Emp emp = new Emp();
            emp.setName(empVO.getName())
                    .setSalary(Double.parseDouble(empVO.getSalary()))
                    .setAge(Integer.parseInt(empVO.getAge()))
                    .setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(empVO.getBirthday()))
                    .setDeptId(Integer.parseInt(empVO.getDeptId()));
            System.out.println(emp);
            //调用dao的方法
            empDao.insertEmp(emp);
            conn.commit();

        } catch (Exception e) {
            System.out.println("=======================");
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwables) {

            }
        } finally {
            JDBCUtil.release(conn);
        }


        return 0;
    }

    @Override
    public int deleteEmpById(String id) {
        Connection conn = null;
        try {

            //回去链接 手动设置提交方式
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            //将字符串转化为Integer
            empDao.deleteEmpById(Integer.parseInt(id));
            //提交事务
            conn.commit();


        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn);
        }


        return 0;
    }

    @Override
    public EmpDTO selectEmpById(String id) {
        Connection conn = null;
        EmpDTO emp = null;
        try {

            //回去链接 手动设置提交方式
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            //将字符串转化为Integer
            emp = empDao.selectEmpById(Integer.parseInt(id));
            //提交事务
            conn.commit();


        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn);
        }

        return emp;
    }

    @Override
    public int updateEmp(EmpUpdateVO vo) {
        Connection conn = null;

        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            //将empVO的数据疯转到Emp
            Emp emp = new Emp();
            emp.setName(vo.getName())
                    .setId(Integer.parseInt(vo.getId()))
                    .setSalary(Double.parseDouble(vo.getSalary()))
                    .setAge(Integer.parseInt(vo.getAge()))
                    .setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(vo.getBirthday()))
                    .setDeptId(Integer.parseInt(vo.getDeptId()));
            System.out.println(emp);
            //调用dao的方法
            empDao.updateEmp(emp);
            conn.commit();

        } catch (Exception e) {
            System.out.println("=======================");
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException throwables) {

            }
        } finally {
            JDBCUtil.release(conn);
        }


        return 0;
    }
}
